package cn.com.jinhao.hotline.system.service.impl;

import cn.com.jinhao.hotline.commons.base.PageCommon;
import cn.com.jinhao.hotline.commons.base.constant.SystemConstant;
import cn.com.jinhao.hotline.system.entity.TbSysDict;
import cn.com.jinhao.hotline.system.mapper.SysDictMapper;
import cn.com.jinhao.hotline.system.service.SysDictService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysDictServiceImpl implements SysDictService, PageCommon {
    @Resource
    SysDictMapper sysDictMapper;

    public Map<String, Object> querySysDict(String paid, String deptCode,boolean isAll,int pageNo, int pageSize) {
        Example example = new Example(TbSysDict.class);
        Example.Criteria criteria = example.createCriteria();

        String parentCode = "";
        for (SystemConstant.SysDict e : SystemConstant.SysDict.values()) {
            if (e.getCode().equals(paid)) {
                parentCode = e.getName();
            }
        }
        parentCode = parentCode.equals("")?paid:parentCode;

        criteria.andEqualTo("paid",parentCode);

        if (StringUtils.isNotBlank(deptCode)) {
            criteria.andEqualTo("deptCode", deptCode);
        }

        pageNo = isAll?0:pageNo;
        return getResult(example, pageNo, pageSize);
    }

    @Override
    public Map<String, Object> getResult(Example example, int pageNo, int pageSize) {
        HashMap<String, Object> map = new HashMap<>(10);
        if (pageNo != 0 && pageSize != 0) {
            PageHelper.startPage(pageNo, pageSize);
            PageInfo<TbSysDict> pageInfo = new PageInfo<>(sysDictMapper.selectByExample(example));
            map.put("list", pageInfo);
            map.put("total", pageInfo.getTotal());
            map.put("pageSize", pageInfo.getPageSize());
            map.put("pageNo", pageInfo.getPageNum());
        } else {
            List<TbSysDict> dispatches = sysDictMapper.selectByExample(example);
            map.put("list", dispatches);
            map.put("total", dispatches.size());
        }
        return map;
    }
}
